package cn.iocoder.yudao.module.pickbook.service.bookcontent;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pickbook.controller.admin.bookcontent.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.bookcontent.BookContentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.bookcontent.BookContentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 绘本内容 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BookContentServiceImpl implements BookContentService {

    @Resource
    private BookContentMapper bookContentMapper;

    @Override
    public Long createBookContent(BookContentSaveReqVO createReqVO) {
        // 插入
        BookContentDO bookContent = BeanUtils.toBean(createReqVO, BookContentDO.class);
        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        bookContent.setId(id);
        bookContentMapper.insert(bookContent);

        // 返回
        return bookContent.getId();
    }

    @Override
    public void updateBookContent(BookContentSaveReqVO updateReqVO) {
        // 校验存在
        validateBookContentExists(updateReqVO.getId());
        // 更新
        BookContentDO updateObj = BeanUtils.toBean(updateReqVO, BookContentDO.class);
        bookContentMapper.updateById(updateObj);
    }

    @Override
    public void deleteBookContent(Long id) {
        // 校验存在
        validateBookContentExists(id);
        // 删除
        bookContentMapper.deleteById(id);
    }

    @Override
        public void deleteBookContentListByIds(List<Long> ids) {
        // 删除
        bookContentMapper.deleteByIds(ids);
        }


    private void validateBookContentExists(Long id) {
        if (bookContentMapper.selectById(id) == null) {
            throw exception(BOOK_CONTENT_NOT_EXISTS);
        }
    }

    @Override
    public BookContentDO getBookContent(Long id) {
        return bookContentMapper.selectById(id);
    }

    @Override
    public PageResult<BookContentDO> getBookContentPage(BookContentPageReqVO pageReqVO) {
        return bookContentMapper.selectPage(pageReqVO);
    }

}